//
//  ViewController.m
//  contoct
//
//  Created by kmdai on 15/1/9.
//  Copyright (c) 2015年 kmdai. All rights reserved.
//

#import "ViewController.h"
#define  VIEW_Y 0;
@interface ViewController (){
    int _viewY ;
    int _viewTag;
    UIScrollView *scrollView;
    XibClass *_xibtest;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, 375, 603)];
    _viewY=VIEW_Y;
    _viewTag=10;
    //    xib=[[XibClass alloc]init];
    // Do any additional setup after loading the view, typically from a nib.
    _itemAdd.action=@selector(add:);
    _itemRem.action=@selector(remove:);
    _xibtest=[[XibClass alloc]init];
    [_xibtest setL:self];
    CGSize s=CGSizeMake(375, 603);
    scrollView.contentSize=s;
    //    scrollView.userInteractionEnabled=YES;
    [self.view addSubview:scrollView];
    scrollView.showsVerticalScrollIndicator=NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)add:(UIBarButtonItem*)btn{
    NSLog(@"add-----------");
    
    //加载xib文件
    //    NSArray *views= [[NSBundle mainBundle]loadNibNamed:@"XibTest" owner:self options:nil];
    //    UIView *v=views[0];
    UIView *v=_xibtest.getView;
    //    UIButton *delete=(UIButton *) [v viewWithTag:5] ;
    //    [delete addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
    CGRect f=v.frame;
    f.origin.x=375;
    f.origin.y=_viewY;
    v.frame=f;
    v.alpha=0;
    v.tag=_viewTag++;
    _viewY+=50;
    [UIView animateWithDuration:1 animations:^{
        CGRect f=v.frame;
        f.origin.x=0;
        v.frame=f;
        v.alpha=1;
    }];
    if(_viewY>600){
        CGSize s=CGSizeMake(375, _viewY);
        scrollView.contentSize=s;
    }
    [self->scrollView addSubview:v];
    //    [self.view addSubview:v];
}
-(void)remove:(UIBarButtonItem*)btn{
    NSLog(@"remove-----------");
    UIView *v=[self.view viewWithTag:--_viewTag];//根据tag获得view
    NSLog(@"%ld",v.tag);
    /*
     设置动画，动画执行玩在执行comlpetion
     */
    [UIView animateWithDuration:1 animations:^{
        CGRect f=v.frame;
        f.origin.x=375;
        v.frame=f;
        v.alpha=0;
    }completion:^(BOOL finished) {
        [v removeFromSuperview];
        if([self->scrollView subviews].count==3){
            _viewY=VIEW_Y;
        }
    }];
    if(_viewY>600){
        _viewY-=50;
        CGSize s=CGSizeMake(375, _viewY);
        scrollView.contentSize=s;
    }else{
         _viewY-=50;
        CGSize s=CGSizeMake(375, 600);
        scrollView.contentSize=s;
    }
}
-(void)btnclick:(UIButton*)btn{
    NSLog(@"click------");
    [btn.superview removeFromSuperview];
}
-(void)btnClickPro:(UIButton *)btn{
    NSLog(@"btnClickPro----");
    int a= (int)[scrollView.subviews indexOfObject:btn.superview];
    [btn.superview removeFromSuperview];
    _viewY-=50;
   [UIView animateWithDuration:0.5 animations:^{
       for(int i=a;i<scrollView.subviews.count;i++){
           UIView *v=(UIView*)scrollView.subviews[i];
           CGRect f=v.frame;
           f.origin.y-=50;
           v.frame=f;
       }
   } completion:^(BOOL finished) {
       if(_viewY>600){
           _viewY-=50;
           CGSize s=CGSizeMake(375, _viewY);
           scrollView.contentSize=s;
       }else{
           _viewY-=50;
           CGSize s=CGSizeMake(375, 600);
           scrollView.contentSize=s;
       }
   }];
    
}
@end

